/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dess.sso.service;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
 * @project-name:dess
 * @package-name:com.hyts.dess.sso.service
 * @author:LiBo/Alex
 * @create-date:2020-07-22 19:50
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class UserAuthService implements UserDetailsService {

    /**
     * 加载用户名和密码信息
     * @param s
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        // 用户名是否为空!
        if(StringUtils.isEmpty(s)){
            throw new RuntimeException("用户名不允许为空!");
        }else{
            // TODO 校验查询相关的查询接口是否
            boolean isFoundUserName = true;
            if(!isFoundUserName){
                throw new UsernameNotFoundException("用户名未找到!");
            }else{
                return new User(s,"$2a$10$O0lEMxDSUU97VxlXFPVNLuyBpylrP2UhNMDCTHrwAL2ERoW9HGx2W",
                        Arrays.asList(new SimpleGrantedAuthority("ROLE_user"), new SimpleGrantedAuthority("ROLE_admin")));
            }
        }
    }

}
